<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 登陆表单 -->
      <el-form ref="loginFormRef" class="login_form" :model="loginForm" :rules="loginFormRule">
        <!-- 账号密码 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>


<script>
export default {
  name: "Login",
  data() {
    return {
      //登陆表单数据对象
      loginForm: {
        username: "admin",
        password: "123456"
      },
      //表单验证规则对象
      loginFormRule: {
        username: [
          { required: true, message: "请输入登陆名称!", trigger: "blur" },
          {
            min: 3,
            max: 5,
            message: "长度在 3 到 5 个字符之间!",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入密码!", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符之间!",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    //点击重置
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    //登陆预先校验
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          this.$message.error("请正确输入!");
        } else {


          //接受多一种方式 catch
          // var that = this;

          // async function queryData() {
          //   var ret = await that.$axios.post("login", that.loginForm);
          //   return ret.data;
          // }
          // queryData()
          //   .then(data => {
          //     console.log(data);
          //   })
          //   .catch(error => {
          //     console.log(error);
          //   });

          var data = await this.$axios.post("login", this.loginForm);
          if (data.data.meta.status !== 200) {
            this.$message.error("不存在的用户名密码哦!");
          } else if (data.data.meta.status == 200) {
            this.$message({
              message: `${data.data.data.username} 欢迎登陆!`,
              type: "success"
            });
            //1将登陆成功之后的token保存到客户端的sessionStorage中
            //  因为项目中除了登陆之外的其他接口必须在登陆之后才能访问
            //  而且网站只应在当前网站打开期间生效所以保存在sessionStorage中而不是localStorage
            //  cookie保存在浏览器端，session保存在服务器端
            window.sessionStorage.setItem('token',data.data.data.token);

            //2用编程式导航跳转到对应的路由,因为不能没登陆就访问
            this.$router.push('/home')
          } else {
            this.$message.error("未知错误！");
          }




        }
      });
    }
  }
};
</script>










<style lang='less' scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 6px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translateX(-50%) translateY(-50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .btns {
    display: flex;
    justify-content: space-around;
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
